package com.kc.sjwh.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.bo.UserBo;
import com.kc.sjwh.entity.po.Ry;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.service.RyService;

/**
 * 人员 action
 * @author zhangguihua(hua7381@163.com)
 * date: 2018年2月22日
 */
@Controller
@RequestMapping("ry")
public class RyAction extends BaseAction {
	static Logger logger = Logger.getLogger(RyAction.class);
	
	@Autowired
	RyService service;
	
	//打开列表页
	@RequestMapping("list")
	public String list(HttpServletRequest req) {
		return "ry/ryList";
	}
	
	//分页查询
	@ResponseBody
	@RequestMapping("getPage")
	public String getPage(HttpServletRequest req, int pageSize, int pageNum) {
		Map<String, String> m = super.getParamMap(req);

		if(!SecurityUtils.getSubject().hasRole("admin")) {
			m.put("dwdm", getLoginUser().getSsdwdm());
		}
		
		Page<Ry> page = service.getPage(pageSize, pageNum, m);
		return JSON.toJSONString(page);
	}

	//打开表单
	@RequestMapping("form")
	public String form(HttpServletRequest req, String act, String id) {
		req.setAttribute("act", act);
		req.setAttribute("id", id);
		req.setAttribute("actName", "add".equals(act)?"新增":"修改");
		req.setAttribute("loginUser", super.getLoginUser());
		return "ry/ryForm";
	}

	//表单加载
	@ResponseBody
	@RequestMapping("load4Update")
	public String load4Update(HttpServletRequest req, String id) throws Exception {
		Ry one = service.load4Update(id);
		return JSON.toJSONString(one);
	}
	
	@ResponseBody
	@RequestMapping("add")
	public String add(Ry one, String act) {
		UserBo user = getLoginUser();
		one.setCjr(user.getMc());
		if(StringUtils.isEmpty(one.getSsdwdm())) {
			one.setSsdwdm(user.getSsdwdm());
			one.setSsdwmc(user.getSsdwmc());
		}
		service.add(one);
		return JSON.toJSONString(null);
	}
	
	@ResponseBody
	@RequestMapping("update")
	public String update(Ry one, String act) {
		one.setGxr(getLoginUser().getMc());
		service.update(one);
		return JSON.toJSONString(null);
	}
	
	//删除
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest req, String id) {
		service.delete(id);
		return JSON.toJSONString(null);
	}
	
}
